package com.oak.broke.config.mybatis.encript.encrypt;

import com.oak.broke.utils.sm4.SM4Util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class Sm4StringSecretHandler implements ISm4SecretHandler {

    @Override
    public boolean support(Class<?> clazz) {
        return String.class.equals(clazz);
    }

    @Override
    public Object encryptObject(Object obj) {
        if (obj == null || StringUtils.isBlank(obj.toString())) {
            return obj;
        }
        try {
            return SM4Util.encryptDataECB(obj.toString());
        } catch (Exception e) {
            log.error("数据【{}】加密异常，原因:", obj, e);
            return obj;
        }
    }

    @Override
    public Object decryptObject(Object obj) {
        if (obj == null || StringUtils.isBlank(obj.toString())) {
            return obj;
        }
        try {
            return SM4Util.decryptDataECB(obj.toString());
        } catch (Exception e) {
            log.error("数据【{}】解密异常，原因:", obj, e);
            return obj;
        }
    }
}
